import { request } from 'umi';
import type { RequestData } from '@ant-design/pro-table';

const route = '/system_info';

export async function getNodeOptions() {
  return new Promise<OptionEntity[]>((resolve, reject) => {
    getNodes()
      .then((res) => {
        const { data } = res;
        resolve(data ? data.map((item) => ({ label: item.node_name, value: item.node_name })) : []);
      })
      .catch((e) => {
        reject(e);
      });
  });
}

export async function getNodes() {
  return await request<RequestData<NodeEntity>>(`${route}/list_node_info`, {
    method: 'GET',
  });
}

export async function getMetrics(nodeName: string[], metricNames: string[]) {
  return await request(`${route}/query_metrics`, {
    method: 'GET',
    params: {
      node_lists: nodeName.join(','),
      metric_names: metricNames.join(','),
    },
    skipErrorHandler: true,
  });
}
